﻿using UnityEngine;
using System.Collections.Generic;

public class CharacterJoin : MonoBehaviour
{
	public class Container
	{
		public Container(Transform parent)
		{
			list = new List<Character>( parent.GetComponentsInChildren<Character>() );
		}
		public Character Pop()
		{
				if (0 < list.Count)
				{
					var result = list [list.Count - 1];
					list.RemoveAt (list.Count - 1);
					return result;
				}
				return null;
		}

		public List<Character> list;
	}
	void Update ()
	{
		var objs = new Container (transform);
		for(;;)
		{
			var cur = objs.Pop ();
			if (cur == null)
			{
				break;
			}
			foreach (var other in objs.list)
			{
				if (cur.CheckJoin ( other ))
				{
					other.Join (cur);
					return;
				}
			}
		}
	}
}
